
package com.hlkj.pay.controller.order;
/*
 * Hlpay-Plus aggregate payment system.
 * Copyright (c) 2024-2025 Hlpay Team Copyright has the right of final interpretation.
 */

import java.util.Collections;
import java.util.List;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.hlkj.framework.common.pojo.CommonResult;
import com.hlkj.framework.common.pojo.PageResult;
import com.hlkj.framework.common.util.collection.CollectionUtils;
import com.hlkj.pay.app.order.ITransferOrderAppService;
import com.hlkj.pay.controller.BaseController;
import com.hlkj.pay.dto.order.OrderTransferDto;
import com.hlkj.pay.dto.order.TransferOrderQueryDto;
import com.hlkj.pay.infrastructure.model.merchant.MerchantReportInfoDO;
import com.hlkj.pay.infrastructure.model.order.OrderTransferDO;
import com.hlkj.pay.service.merchant.IMerchantReportService;
import com.hlkj.pay.service.order.ITransferOrderService;
import com.hlkj.pay.vo.order.req.OrderTransferPreReq;
import com.hlkj.pay.vo.order.req.OrderTransferReq;
import com.hlkj.pay.vo.order.req.TransferOrderPageQueryReq;
import com.hlkj.pay.vo.order.resp.OrderTransferResp;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.dreamlu.mica.core.utils.BeanUtil;

/**
 * @author HlpayTeam
 * @date 2024/10/23 11:36
 */
@Tag(name = "汇电通转账订单管理")
@RestController
@RequestMapping("/v1/order/transfer")
@RequiredArgsConstructor
@Slf4j
public class TransferOrderController extends BaseController {

    private final ITransferOrderAppService transferOrderAppService;

    private final ITransferOrderService transferOrderService;
    private final IMerchantReportService merchantReportService;

    @Operation(summary = "订单查询")
    @GetMapping("/page")
    CommonResult<OrderTransferResp> queryPayOrderPage(TransferOrderPageQueryReq payOrderPageQueryReq) {
        TransferOrderQueryDto commonOrderQueryDto = BeanUtil.copy(payOrderPageQueryReq, TransferOrderQueryDto.class);
        processQueryDate(payOrderPageQueryReq, commonOrderQueryDto);
        PageResult<OrderTransferDto> page = transferOrderService.queryPage(commonOrderQueryDto);
        List<OrderTransferDto> list = page.getList();
        if (CollectionUtils.isEmpty(list)) {
            return buildPageResult(page, Collections.emptyList());
        }
        List<OrderTransferResp> payOrderRespList = cn.hutool.core.bean.BeanUtil.copyToList(list, OrderTransferResp.class);
        // processOrderList(payOrderRespList);
        return buildPageResult(page, payOrderRespList);
    }

    @Operation(summary = "订单详情")
    @GetMapping("/detail")
    CommonResult<OrderTransferResp> queryPayOrderDetail(@RequestParam("payOrderNo") String payOrderNo) {
        OrderTransferDO orderTransferDO = transferOrderService.queryPayOrder(payOrderNo);
        if (orderTransferDO == null) {
            return CommonResult.success(null);
        }
        transferOrderAppService.queryPayStatus(orderTransferDO);
        OrderTransferResp payOrderDetailResp = BeanUtil.copyProperties(orderTransferDO, OrderTransferResp.class);
        MerchantReportInfoDO inMerchantReportInfoDO=merchantReportService.detailByUserId(orderTransferDO.getInUserId());
        MerchantReportInfoDO outMerchantReportInfoDO=merchantReportService.detailByUserId(orderTransferDO.getOutUserId());
        if(inMerchantReportInfoDO!=null){
            payOrderDetailResp.setInUserName(inMerchantReportInfoDO.getName());
            payOrderDetailResp.setInVirtualAccountNo(inMerchantReportInfoDO.getVirtualAccountNo());
            payOrderDetailResp.setBankName(inMerchantReportInfoDO.getBankName());
        }
        if(outMerchantReportInfoDO!=null){
            payOrderDetailResp.setOutUserName(outMerchantReportInfoDO.getName());
            payOrderDetailResp.setOutVirtualAccountNo(outMerchantReportInfoDO.getVirtualAccountNo());
        }
        return CommonResult.success(payOrderDetailResp);
    }

    @Operation(summary = "提交预订单")
    @PostMapping("/submitPreOrder")
    CommonResult<OrderTransferResp> submitPreOrder(@RequestBody @Validated OrderTransferPreReq orderTransferPreReq) {
        return transferOrderAppService.submitPreOrder(orderTransferPreReq);
    }

    @Operation(summary = "提交订单")
    @PostMapping("/submitOrder")
    CommonResult<OrderTransferResp> submitOrder(@RequestBody @Validated OrderTransferReq orderTransferReq) {
        return transferOrderAppService.submitOrder(orderTransferReq);
    }
    @Operation(summary = "状态同步")
    @GetMapping("/status")
    CommonResult<Void> status(@RequestParam("payOrderNo") String payOrderNo) {
        OrderTransferDO orderTransferDO = transferOrderService.queryPayOrder(payOrderNo);
        if (orderTransferDO == null) {
            return CommonResult.success(null);
        }
        transferOrderAppService.queryPayStatus(orderTransferDO);
        return CommonResult.success();
    }
}
